using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace ChuyenDongVatLy
{ 
   
    public class ConLacDon : Microsoft.Xna.Framework.GameComponent
    {
        private string chieudail;
        private string tanso;
        private string gocnghieng;
        private string giatoc;

        private BaseObject _object;
        public Texture2D imageTinh;
        public Texture2D imageDong;

        private Texture2D picPlay;
        private Texture2D picStop;
        private Texture2D picPause;
        private Texture2D picControl;

        private Texture2D picPlay2;
        private Texture2D picStop2;
        private Texture2D picPause2;
        private Texture2D picControl2;
        private SpriteFont font1;

        private bool _isPause = false;
        private bool _isPlay = false;
        private bool _isStop = false;
        private bool _isControl = false;

        private bool isPause = false;

        MouseGame mouse;    
        BaseObject _vatthe;
        Game1 game;
        float BienDoDaoDong = 200;
        double gocLechBanDau= Math.PI/3;
        float chieuDaiSoiDay= 100;
        Vector2 S0 = new Vector2(300, 118);
        Texture2D _imageLine;
        float Gravity = ConstPhysic.Gravity;
        //
        private Texture2D picKhung;
        private Texture2D picKhung2;
        private Texture2D bg;
        private Texture2D picThanhGo;


        public ConLacDon(Game1 game1)
            : base(game1)
        {
            this.game = game1;
            _vatthe = new BaseObject();
            mouse = new MouseGame(game1);        
        }
        public ConLacDon(Game1 game1,double gocLechBanDau1,float chieuDaiSoiDay1,Vector2 vitribandau1):base(game1)
        {
            this.game = game1;
            this.gocLechBanDau = gocLechBanDau1;
            this.chieuDaiSoiDay = chieuDaiSoiDay1;
            this.S0 = vitribandau1;
        }
        public override void Initialize()
        {          
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            CreateForm();
            MouseOverButton();
            CheckButton();
            ChuyenDong(gameTime);
            base.Update(gameTime);
        }
        public void LoadContent(ContentManager Content)
        {
            picControl = Content.Load<Texture2D>("Button\\b_Control");
            picPlay = Content.Load<Texture2D>("Button\\b_Play");
            picPause = Content.Load<Texture2D>("Button\\b_Pause");
            picStop = Content.Load<Texture2D>("Button\\b_Stop");


            picControl2 = Content.Load<Texture2D>("Button\\a_Control");
            picPlay2 = Content.Load<Texture2D>("Button\\a_Play");
            picPause2 = Content.Load<Texture2D>("Button\\a_Pause");
            picStop2 = Content.Load<Texture2D>("Button\\a_Stop");

            _vatthe.Picture = Content.Load<Texture2D>("ball");
            _imageLine = Content.Load<Texture2D>("CLD\\Conlacdon");
            imageTinh = Content.Load<Texture2D>("CLD\\tinh");
            imageDong = Content.Load<Texture2D>("CLD\\dong");

            font1 = Content.Load<SpriteFont>("font");

            picKhung = Content.Load<Texture2D>("DDDH\\khung");
            picKhung2 = Content.Load<Texture2D>("DDDH\\khung2");
            bg = Content.Load<Texture2D>("DDDH\\bg");
            picThanhGo = Content.Load<Texture2D>("thanhgo");
        }
        public void ChuyenDong(GameTime gameTime)
        {
            if (isPause==false)
            {
                TimeGame._gameTime += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                _vatthe.AngularThrow = gocLechBanDau * Math.Cos(Math.Sqrt(Gravity / (chieuDaiSoiDay / 100)) * TimeGame._gameTime);
                _vatthe.Pos = new Vector2((float)(300 * Math.Sin(_vatthe.AngularThrow)), (float)(300 * Math.Cos(_vatthe.AngularThrow))) + S0;
            }           
        }
        
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(bg, new Rectangle(0, 0, 900, 700), Color.White);//bg
            spriteBatch.Draw(picKhung, new Rectangle(0, 0, 900, 100), Color.White);//khung
            spriteBatch.Draw(picKhung2, new Rectangle(630, 100, 180, 700), Color.White);//khung2
            if (_isPlay)
                spriteBatch.Draw(picPlay2, ConstPhysic.btnPlay2, Color.White);
            else
                spriteBatch.Draw(picPlay, ConstPhysic.btnPlay, Color.White);
            if (_isStop)
                spriteBatch.Draw(picStop2, ConstPhysic.btnStop2, Color.White);
            else
                spriteBatch.Draw(picStop, ConstPhysic.btnStop, Color.White);
            if (_isPause)
                spriteBatch.Draw(picPause2, ConstPhysic.btnPause2, Color.White);
            else
                spriteBatch.Draw(picPause, ConstPhysic.btnPause, Color.White);
            if (_isControl)
                spriteBatch.Draw(picControl2, ConstPhysic.btnControl2, Color.White);
            else
                spriteBatch.Draw(picControl, ConstPhysic.btnControl, Color.White);
            spriteBatch.Draw(_imageLine,new Vector2(S0.X,S0.Y),new Rectangle(0,0,(int)300,50),Color.White,(float)(-_vatthe.AngularThrow+Math.PI/2*3),new Vector2(300,28),1.0f,SpriteEffects.None,0);
            spriteBatch.Draw(picThanhGo,new Rectangle(250,100,100,20),Color.White);
            spriteBatch.Draw(imageTinh,ConstPhysic.tinh,Color.White);
            spriteBatch.Draw(imageDong,ConstPhysic.dong,Color.White);
            spriteBatch.DrawString(font1,chieuDaiSoiDay.ToString() + "(cm)", new Vector2(700,33),Color.Blue);

            spriteBatch.DrawString(font1, Math.Round(gocLechBanDau,2,MidpointRounding.AwayFromZero).ToString() + "(rad)", new Vector2(700, 127), Color.Blue);
            spriteBatch.DrawString(font1,Math.Round(Gravity,1,MidpointRounding.AwayFromZero).ToString() + "(cm/s2)", new Vector2(700, 180), Color.Blue);
            spriteBatch.DrawString(font1, Math.Round(_vatthe.AngularThrow, 2, MidpointRounding.AwayFromZero).ToString() + "(rad)", new Vector2(700, 332), Color.Blue);
            double f = (Math.Sqrt(Gravity / (chieuDaiSoiDay / 100))) / (2 * Math.PI);
            spriteBatch.DrawString(font1, Math.Round(f,2,MidpointRounding.AwayFromZero).ToString() + "(Hz)", new Vector2(700, 83), Color.Blue);
        }

        public void DrawButton(SpriteBatch sp)
        {
            if (_isPlay)
                sp.Draw(picPlay2, ConstPhysic.btnPlay2, Color.White);
            else
                sp.Draw(picPlay, ConstPhysic.btnPlay, Color.White);
            if (_isStop)
                sp.Draw(picStop2, ConstPhysic.btnStop2, Color.White);
            else
                sp.Draw(picStop, ConstPhysic.btnStop, Color.White);
            if (_isPause)
                sp.Draw(picPause2, ConstPhysic.btnPause2, Color.White);
            else
                sp.Draw(picPause, ConstPhysic.btnPause, Color.White);
            if (_isControl)
                sp.Draw(picControl2, ConstPhysic.btnControl2, Color.White);
            else
                sp.Draw(picControl, ConstPhysic.btnControl, Color.White);          
        }

        public void CheckButton()
        {
            if (mouse.CheckMouse(ConstPhysic.btnPlay) && Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                _isPlay = true;
                isPause = false;
            }
            if (mouse.CheckMouse(ConstPhysic.btnPause) && Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                isPause = true;
            }
            if (mouse.CheckMouse(ConstPhysic.btnStop) && Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                isPause = true;
                ReSet();
            }
        }
        public void MouseOverButton()
        {
            if (mouse.CheckMouse(ConstPhysic.btnControl)) _isControl = true;
            else _isControl = false;
            if (mouse.CheckMouse(ConstPhysic.btnPause)) _isPause = true;
            else _isPause = false;
            if (mouse.CheckMouse(ConstPhysic.btnPlay)) _isPlay = true;
            else _isPlay = false;
            if (mouse.CheckMouse(ConstPhysic.btnStop)) _isStop = true;
            else _isStop = false;
        }
        public void SetValue(string chieudaiday, string goclech, string giatocg)
        {
            this.chieudail = chieudaiday;
            this.gocnghieng = goclech;
            this.giatoc = giatocg;

            float chieudaiday1= float.Parse(chieudaiday);            
            float goclech1= float.Parse(goclech);
            float giatocg1= float.Parse(giatocg);
            this.chieuDaiSoiDay = chieudaiday1;
            this.Gravity = giatocg1;
            this.gocLechBanDau = (float)(goclech1*Math.PI);
        }
        public void SetValue2(bool Createform)
        {
            TimeGame._ShowForm = Createform;
        }
        public void CreateForm()
        {
            if (mouse.CheckMouse(ConstPhysic.btnControl) && TimeGame._ShowForm && Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                frConlacdon fr = new frConlacdon();
                fr.GetMyValue = new frConlacdon.GetString(SetValue);
                fr.GetMyValue2 = new frConlacdon.GetString2(SetValue2);
                fr.Show();
                TimeGame._ShowForm = false;
            }            
        }
        public void ReSet()
        {
            _vatthe.AngularThrow = gocLechBanDau;

        }

    }
}
